﻿using iTextSharp.text;
using iTextSharp.text.pdf;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace drawing_print.util
{
    class Images
    {
        /// <summary>
        /// 图片转为PDF
        /// </summary>
        /// <param name="source"></param>
        /// <param name="output"></param>
        public static void imageToPdf(string source, string output)
        {
            iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(source);
            using (FileStream fs = new FileStream(output, FileMode.Create, FileAccess.Write, FileShare.None))
            {
                using (Document doc = new Document(image))
                {
                    using (PdfWriter writer = PdfWriter.GetInstance(doc, fs))
                    {
                        //writer.CompressionLevel = 0;
                        writer.SetFullCompression();
                        writer.SetPdfVersion(iTextSharp.text.pdf.PdfWriter.PDF_VERSION_1_7);
                        //writer.CompressionLevel = PdfStream.NO_COMPRESSION; 

                        doc.Open();
                        image.SetAbsolutePosition(0, 0);
                        doc.SetPageSize(new iTextSharp.text.Rectangle(0, 0, image.Width, image.Height, 0));
                        doc.NewPage();

                        writer.DirectContent.AddImage(image, false);
                        doc.Close();
                    }
                }
            }
        }

        /// <summary>
        /// 判断文件类型是否支持
        /// </summary>
        /// <param name="extension"></param>
        /// <returns></returns>
        public static bool isSupport(string extension)
        {
            switch (extension)
            {
                case ".tif":
                    return true;
                case ".png":
                    return true;
                case ".jpg":
                    return true;
                case ".gif":
                    return true;
                case ".bmp":
                    return true;
                case ".tiff":
                    return true;
                default:
                    return false;
            }
        }
    }
}
